package com.ziq.zcamp.main

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import android.view.animation.*
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.SpanUtils
import com.ziq.base.base.BaseDialog
import com.ziq.base.extension.*
import com.ziq.zcamp.databinding.DialogBottomSheetBinding
import com.ziq.zcamp.databinding.ItemBottomSheetBinding

class BottomSheetDialog(context: Context, val list: List<String>, val itemUiCallback:((text:String, span : SpanUtils)->Unit)? = null) : BaseDialog(context) {

    private val binding: DialogBottomSheetBinding by binding()
    var callBack: ActionCallBack? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.root
        binding.btnCancel.click {
            dismiss()
        }

        binding.recyclerView.layoutManager = LinearLayoutManager(context)
        binding.recyclerView.adapter = ActionAdapter()
        setAnimateView(binding.root)
    }

    override fun initWidth(): Int {
        return ViewGroup.LayoutParams.MATCH_PARENT
    }

    override fun initGravity(): Int {
        return Gravity.BOTTOM or Gravity.CENTER
    }

    inner class ActionAdapter : RecyclerView.Adapter<BindingViewHolder<ItemBottomSheetBinding>>(){

        override fun getItemCount(): Int {
            return list.size
        }

        override fun onCreateViewHolder(
            parent: ViewGroup,
            viewType: Int
        ): BindingViewHolder<ItemBottomSheetBinding> {
            return createBindingViewHolder(parent)
        }

        override fun onBindViewHolder(
            holder: BindingViewHolder<ItemBottomSheetBinding>,
            position: Int
        ) {
            val item = list[position]
            holder.itemView.click {
                callBack?.onActionClick(this@BottomSheetDialog, position, item)
            }
            if(itemUiCallback == null){
                holder.binding.text.text = item
                holder.binding.text.setTextColor(Color.parseColor("#333333"))
            } else {
                val span = SpanUtils.with(holder.binding.text)
                itemUiCallback.invoke(item, span)
                span.create()
            }
        }

    }


    class Builder(val context: Context) {

        var list: List<String>? = null
        var callBack: ActionCallBack? = null
        var itemUiCallback:((text:String, span : SpanUtils)->Unit)? = null

        fun setList(data: List<String>, cb: ActionCallBack) : Builder{
            list = data
            callBack = cb
            return this
        }

        fun setItemUiCallback(callback:((text:String, span : SpanUtils)->Unit)) : Builder{
            this.itemUiCallback = callback
            return this
        }


        fun build(): BottomSheetDialog {
            val dialog = BottomSheetDialog(context, list!!, itemUiCallback)
            dialog.callBack = callBack
            return dialog
        }

    }

    interface ActionCallBack{
        fun onActionClick(dialog: BottomSheetDialog, position: Int, text : String)
    }
}